package com.clanteam.javaking.fic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/*
 * ChunkData represents groups of 3 digits, so the value 
 * of ChunkData will never exceed 999
 */

public class ChunkData {
	
	final long seventy = 7, hundred = 7, million = 7, billion = 7; // 7 counters
    final long thousand = 8, trillion = 8; // 8 counters
    final long quadrillion = 11, quintillion = 11; // 11 counters
	
	public List<LookupValue> values = new ArrayList<LookupValue>();
	public long positionIndicator; //for numbers < 10^3, 0; between 10^3 and 10^6, 3
	
	public void add(LookupValue lv) {
		values.add(lv);
		if (lv.power > 2) {
			long powMult3 = lv.power / 3;
			positionIndicator = powMult3*3;
		}
	}
	
	public long getValue(){
		/*
		 * Returns the value stored in ChunkData.
		 */
		long value = 0 ;
		for (Iterator<LookupValue> iterator = values.iterator(); iterator.hasNext();) {
			LookupValue type = iterator.next();
			value += type.getValue();

		}

		//long bigMultiplier = (long)Math.pow(10, positionIndicator);

		//value = value * bigMultiplier ;
		//value *= (long)Math.pow(10, positionIndicator);
		
		return value;
	}
	
	public long getCharacterCount() {
		long charCount = 0 ;
		for (Iterator<LookupValue> iterator = values.iterator(); iterator.hasNext();) {
			LookupValue lv = iterator.next();
			if (lv.getValue() == 10) {
				LookupValue next = (LookupValue) iterator.next();
				if (lv == next) {
					throw new RuntimeException("Something is Terribly, Terribly Wrong.");
				}
				long lookup = next.getValue() + lv.getValue();
				lookup = LookupValue.lookup(lookup);
				charCount += lookup;
			}
			else charCount += lv.getCharacterCount();

		}
		charCount += addPosition();
		return charCount ;

	}
	private long addPosition() {
		switch ((int)positionIndicator) {
			case 3:
				return thousand;
			case 6:
				return million;
			case 9:
				return billion;
			case 12:
				return trillion;
			case 15:
				return quadrillion;
			case 18:
				return quintillion;
			default:
				return 0;
		}
	}
	
}
